package ua.yandex.skipass;

import java.util.Calendar;

public class DefaultPassChecker implements PassChecker {

    @Override
    public boolean checkPass(SkiPass skiPass, Calendar checkedTime, System system) {
        return checkExpirationDate(skiPass, checkedTime)
                && passValidAt(skiPass, checkedTime)
                && checkDoesCardBlocked(skiPass, system)
                && checkClimbs(skiPass);
    }

    private boolean checkExpirationDate(SkiPass skiPass, Calendar checkedTime) {
        Calendar passExpirationDate = skiPass.getExpirationDate();
        return passExpirationDate.after(checkedTime);
    }

    private boolean passValidAt(SkiPass skiPass, Calendar checkedTime) {
        return skiPass.validAt(checkedTime);
    }

    private boolean checkDoesCardBlocked(SkiPass skiPass, System system) {
        return system.doesCardBlocked(skiPass.getId());
    }

    private boolean checkClimbs(SkiPass skiPass) {
        if (skiPass instanceof PassLimitedClimbs) {
            if (((PassLimitedClimbs) skiPass).getNumberOfClimbs() <= 0) {
                return false;
            }
            ((PassLimitedClimbs) skiPass).decreaseNumberOfClimbs();
        }
        return true;
    }
}
